//
//  ShareWeibo.swift
//  BMWShareSDK
//
//  Created by Li Shengbing, (Shengbing.Li@partner.bmw.com) on 2019/6/11.
//  Copyright © 2019年 Li Shengbing, (Shengbing.Li@partner.bmw.com). All rights reserved.
//  https://www.jianshu.com/p/1fe9dcf79365

import UIKit

class ShareWeibo: NSObject {
    
    static let shareInstance: ShareWeibo = ShareWeibo()

    func share(type: ShareWeiboType) {
        let message = WBMessageObject()
        switch type {
        case .text(text: let _text):
            message.text = _text
            
        case .image(image: let _image, text: let _text):
            message.text = _text ?? ""
            let ext = WBImageObject()
            ext.imageData = _image.jpegData(compressionQuality: 0.8)
            message.imageObject = ext
            
        case .url(url: let _url, text: let _text, description: let _description, thumbImage: let _thumbImage):
            message.text = _text ?? ""
            
            let webUrl = WBWebpageObject()
            webUrl.description = _description
            webUrl.objectID = ""
            webUrl.title = _text
            /// 多媒体内容中缩略图大小不能大于32K
            webUrl.thumbnailData = UIImage.compressImage(_thumbImage!, toByte: 32 * 32).jpegData(compressionQuality: 0.5)
                //_thumbImage?.compresseImageSize(size: CGSize(width: 100, height: 100))?.pngData()
                //UIImage.compressImage(_thumbImage!, toByte: 32 * 1024).jpegData(compressionQuality: 1.0)
            webUrl.webpageUrl = _url
            message.mediaObject = webUrl
            
        case .vedio(text: let _text, description: _, vedioUrl: let _vedioUrl):
            message.text = _text ?? ""
            let vedioObj = WBNewVideoObject()
            vedioObj.addVideo(URL(string: _vedioUrl))
        }
        
        let req = WBSendMessageToWeiboRequest.request(withMessage: message) as! WBBaseRequest
        req.shouldOpenWeiboAppInstallPageIfNotInstalled = false
       _ = WeiboSDK.send(req)
    }
}

extension ShareWeibo: WeiboSDKDelegate {
    
    func didReceiveWeiboResponse(_ response: WBBaseResponse!) {
        if response.isKind(of: WBSendMessageToWeiboResponse.self) {
            let resp = response as! WBSendMessageToWeiboResponse
            
            var result: String = ""
            switch resp.statusCode.rawValue {
            case WeiboSDKResponseStatusCode.success.rawValue:
                result = BMWNSLocalizedString.localizedString("SHARE_WB_CALLBACK_SUCCESS", comment: "Shared")
            case WeiboSDKResponseStatusCode.userCancel.rawValue:
                result = BMWNSLocalizedString.localizedString("SHARE_WB_CALLBACK_USERCANCEL", comment: "Cancelled to send")
            case WeiboSDKResponseStatusCode.sentFail.rawValue:
                result = BMWNSLocalizedString.localizedString("SHARE_WB_CALLBACK_SENTFAIL", comment: "Failed to send")
            case WeiboSDKResponseStatusCode.authDeny.rawValue:
                result  = BMWNSLocalizedString.localizedString("SHARE_WB_CALLBACK_AUTHDENY", comment: "Authorization Failed")
            case WeiboSDKResponseStatusCode.userCancelInstall.rawValue:
                result = BMWNSLocalizedString.localizedString("SHARE_WB_CALLBACK_USERCANCELINSTALL", comment: "Installation was cancelled")
            case WeiboSDKResponseStatusCode.payFail.rawValue:
                result = BMWNSLocalizedString.localizedString("SHARE_WB_CALLBACK_PAYFAIL", comment: "Failed to pay")
            case WeiboSDKResponseStatusCode.shareInSDKFailed.rawValue:
                result = BMWNSLocalizedString.localizedString("SHARE_WB_CALLBACK_SHAREINSDKFAILED", comment: "Failed to share")
            case WeiboSDKResponseStatusCode.unsupport.rawValue:
                result = BMWNSLocalizedString.localizedString("SHARE_WB_CALLBACK_UNSUPPORT", comment: "Unsupported request")
            case WeiboSDKResponseStatusCode.unknown.rawValue:
                result = BMWNSLocalizedString.localizedString("SHARE_WB_CALLBACK_UNKNOWN", comment: "Unknown error")
            default:
                break
            }
            
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.25, execute: {
                if BMWShareManager.shareComplete != nil {
                    BMWShareManager.shareComplete!(result)
                }
            })
        }
    }
    
    func didReceiveWeiboRequest(_ request: WBBaseRequest!) {
    }
}


public enum ShareWeiboType {
    case text(text: String)
    case image(image: UIImage, text: String?)
    case url(url: String, text: String?, description: String?, thumbImage: UIImage?)
    case vedio(text: String?, description: String?, vedioUrl: String)
}
